import { createRouter, createWebHistory } from 'vue-router'
import Blog from '@/views/Blog.vue'
import Menu from '@/views/Menu.vue'
import TimeLine from '@/views/TimeLine.vue'
import Test from '@/views/Test.vue'
import DefaultLayout from '@/layouts/DefaultLayout.vue'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 配置 NProgress
NProgress.configure({
  easing: 'ease', // 缓冲动画类型，可选值如'ease'、'linear'等
  showSpinner: false, // 是否显示环形进度动画
});

// 记录已访问的路由
const visitedRoutes = new Set<string>();

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      component: DefaultLayout,
      children: [
        {
          path: 'blog',
          name: 'Blog',
          component: Blog
        },
        {
          path: 'menu',
          name: 'menu',
          component: Menu
        },
        {
          path: 'timeline',
          name: 'TimeLine',
          component: TimeLine
        },
        {
          path: '',
          name: 'test',
          component: Test
        },
      ]
    }
  ],
})

router.beforeEach((to, from, next) => {
  // 只在首次访问路由时显示进度条
  if (!visitedRoutes.has(to.path)) {
    NProgress.start();
    visitedRoutes.add(to.path);
  }
  next();
})

router.afterEach((to) => {
  // 如果路由包含 `id` 参数，则不滚动到顶部
  if (!to.hash) {
    window.scrollTo(0, 0); // 滚动到顶部
  }
  NProgress.done(); // 结束进度条
});

export default router 